package com.itheima.jdk;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;

/**
 * 原生jdk的方式发送请求
 */
public class Jdk_Get {
    public static void main(String[] args) throws Exception {
        //获取目标地址(这里注意,http或者https协议一定要记得加)
        String url1 = "http://www.itcast.cn";
        //转换成一个url对象
        URL url = new URL(url1);
        //通过url对象获取远程链接
        HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
        //设置请求参数,请求头信息
        urlConnection.setRequestMethod("GET");
        //获取数据,先是获取输入流
        InputStream inputStream = urlConnection.getInputStream();
        //进行循环读取
        int len=0;
        byte[] bytes = new byte[1024];
        while ((len=inputStream.read(bytes))!=-1){
            System.out.println(new String(bytes,0,len));
        }
        //关闭资源
        inputStream.close();

    }
}
